Cluster3D <- read.arff("Cluster3D.arff")
ggplot(data = Cluster3D, aes(x = x, y = y)) +
geom_point()

ggplot(data = Cluster3D, aes(x = x, y = z)) +
geom_point()

ggplot(data = Cluster3D, aes(x = y, y = z)) +
geom_point()

attach(Cluster3D)
scatterplot3d::scatterplot3d(x = x, y = y, z = z)

plotly::plot_ly(x = x, y = y, z = z, type="scatter3d")
## No scatter3d mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
m <- Cluster3D
N <- nrow(m)
K <- matrix(0, nrow=N, ncol=N)
for (i in 1:N) {
distances <- apply(m, 1, function(r) sum((r - m[i,])^2))
closest_4 <- order(distances)[2:5]
K[i,closest_4] <- exp(-distances[closest_4]/.1)
K[closest_4,i] <- exp(-distances[closest_4]/.1)
}
# create D and A
D <- diag(rowSums(K))/sum(K)
A <- apply(K, 1, function(r) r/sum(r)) %>% t
# create symmetric matrix
S <- D^{1/2} %*% A %*% solve(D^{1/2})
# compute eigenvalues and grab first three
eig.out <- eigen(S)
ind <- order(eig.out$values %>% abs, decreasing = T)
q <- eig.out$vectors[,ind][,1:3]
lambdas <- eig.out$values[ind][1:3]
r <- solve(D^{1/2}) %*% q
# let's just check r1 and lambda1
#r[,1]
#lambdas[1]
# 2-d projection
t_vals <- c(1,1000, 10000)
for (t in t_vals) {
y <- cbind(lambdas[2]^t*r[,2], lambdas[3]^t*r[,3])
g <- ggplot(data=data.frame(y1=y[,1], y2=y[,2])) +
geom_point(aes(x=y1, y=y2)) +
xlim(-2,2) + ylim(-2,2) + ggtitle(paste("time", t))
print(g)
}



knn <- kmeans(x = y, centers = 4)
rm(y)
Cluster3D$assignment <- as.factor(knn$cluster)
ggplot(Cluster3D, aes(x = y, y = z, color = assignment)) +
geom_point()

attach(Cluster3D)
## The following objects are masked from Cluster3D (pos = 3):
##
## x, y, z
plotly::plot_ly(data = Cluster3D, x = x, y = y, z = z, type="scatter3d", color = assignment)
## No scatter3d mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode